package org.dtrd.modules.flup.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.flup.entity.bean.FlupTemplateDetail;
import org.dtrd.modules.flup.entity.bean.FlupTemplateListInfo;
import org.dtrd.modules.flup.entity.request.FlupTemplateListRequest;
import org.dtrd.modules.flup.entity.request.FlupTemplateProjectRlRequest;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupAppointmentRecordService;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupTemplateService;
import org.dtrd.modules.flup.service.IDtrdRlFlupTemplateProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 复诊模版 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-21
 */
@Api(tags = "复诊随访记录")
@RestController
@RequestMapping("/flup/template/")
public class FlupTemplateController extends BaseController {

    @Autowired
    private IDtrdEntRdFlupTemplateService flupTemplateService;
    @Autowired
    private IDtrdRlFlupTemplateProjectService flupTemplateProjectRlService;
    @Autowired
    private BaseCommonService baseCommonService;
    @Autowired
    private IDtrdEntRdFlupAppointmentRecordService appointmentRecordService;

    /**
     * 当复诊的机构不为空时，只返回该机构下的复诊模板
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "查询复诊模版列表", response = FlupTemplateListInfo.class)
    @PostMapping("list")
    public Result<?> getFlupTemplateList(@RequestBody FlupTemplateListRequest request){
        List<FlupTemplateListInfo> list = flupTemplateService.getFlupTemplateList(request);
        String logContent = String.format("查询复诊模版列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }


    @ApiOperation("新增或修改复诊模版")
    @PostMapping("edit")
    public Result<?> saveOrUpdateFlupTemplate(@RequestBody FlupTemplateDetail dto){
        if (StrUtil.isBlank(dto.getTemplateName())) {
            return error(STATE_PARAMS_ERROR);
        }
        // 新增时不能有相同的复诊模版名字
        if (flupTemplateService.isInValidTemplateName(dto.getTemplateName(), dto.getDataId(), dto.getOrgCode())) {
            return Result.error("不能有相同名字的模版");
        }
        boolean result = flupTemplateService.saveOrUpdateFlupTemplate(dto);
        String logContent = String.format("新增或修改复诊模版, 编辑复诊模版: %s", dto.getTemplateName());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("设置复诊模版内容")
    @PostMapping("projects")
    public Result<?> saveOrUpdate(@RequestBody @Validated FlupTemplateProjectRlRequest request){
        Integer templateId = request.getTemplateId();
        List<Integer> projectIds = request.getProjectIds();
        // 要是传入的 projectIds 为空返回一个操作成功的状态
        if (CollectionUtil.isEmpty(projectIds)) {
            return Result.OK();
        }
        flupTemplateProjectRlService.clearTemplate(templateId);
        boolean result = flupTemplateService.saveFlupTemplateProject(templateId, projectIds);
        String logContent = String.format("新增或修改复诊模版, 编辑复诊模版id: %d, 复诊模版项目集合: %s", templateId, projectIds);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除复诊模版")
    @GetMapping("remove")
    public Result<?> removeFlupTemplate(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        if (appointmentRecordService.hasTemplateLinkPatient(id)) {
            return Result.error("该模版已有患者预约, 不能删除");
        }
        boolean result = flupTemplateService.removeFlupTemplate(id);
        String logContent = String.format("删除复诊模版, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
